{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "E:\\AItools\\GPT-SoVITS-Inference\n"
     ]
    }
   ],
   "source": [
    "%cd E:\\\\AItools\\\\GPT-SoVITS-Inference"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install -r requirements.txt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "e:\\AItools\\AudioLabeling\\runtime\\lib\\site-packages\\tqdm\\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loading BERT weights from models/pretrained_models/gsv/chinese-roberta-wwm-ext-large\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Some weights of the model checkpoint at models/pretrained_models/gsv/chinese-hubert-base were not used when initializing HubertModel: ['encoder.pos_conv_embed.conv.weight_g', 'encoder.pos_conv_embed.conv.weight_v']\n",
      "- This IS expected if you are initializing HubertModel from the checkpoint of a model models/gsv on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).\n",
      "- This IS NOT expected if you are initializing HubertModel from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).\n",
      "Some weights of HubertModel were not initialized from the model checkpoint at models/pretrained_models/gsv/chinese-hubert-base and are newly initialized: ['encoder.pos_conv_embed.conv.parametrizations.weight.original0', 'encoder.pos_conv_embed.conv.parametrizations.weight.original1']\n",
      "You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loading CNHuBERT weights from models/pretrained_models/gsv/chinese-hubert-base\n",
      "Loading Text2Semantic weights from models/gsv\\Hutao\\hutao-e75.ckpt\n",
      "Using Flash Attention\n",
      "Loading VITS weights from models/gsv\\Hutao\\hutao_e60_s3360.pth\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "e:\\AItools\\AudioLabeling\\runtime\\lib\\site-packages\\torch\\nn\\utils\\weight_norm.py:28: UserWarning: torch.nn.utils.weight_norm is deprecated in favor of torch.nn.utils.parametrizations.weight_norm.\n",
      "  warnings.warn(\"torch.nn.utils.weight_norm is deprecated in favor of torch.nn.utils.parametrizations.weight_norm.\")\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "加载角色成功: Hutao\n",
      "default\n",
      "Set seed to 2834636001\n",
      "分桶处理模式已开启\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "e:\\AItools\\AudioLabeling\\runtime\\lib\\site-packages\\torch\\functional.py:660: UserWarning: stft with return_complex=False is deprecated. In a future pytorch release, stft will return complex tensors for all inputs, and return_complex=False will raise an error.\n",
      "Note: you can still call torch.view_as_real on the complex output to recover the old return format. (Triggered internally at ..\\aten\\src\\ATen\\native\\SpectralOps.cpp:879.)\n",
      "  return _VF.stft(input, n_fft, hop_length, win_length, window,  # type: ignore[attr-defined]\n",
      "Building prefix dict from the default dictionary ...\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "实际输入的参考文本: 我说白术，你不会看不出来吧？难不成你师父，忘了教你这门功夫？\n",
      "DEBUG:jieba_fast:Building prefix dict from the default dictionary ...\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Loading model from cache C:\\Users\\xxoy1\\AppData\\Local\\Temp\\jieba.cache\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "DEBUG:jieba_fast:Loading model from cache C:\\Users\\xxoy1\\AppData\\Local\\Temp\\jieba.cache\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Loading model cost 0.328 seconds.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "DEBUG:jieba_fast:Loading model cost 0.328 seconds.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Prefix dict has been built succesfully.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "DEBUG:jieba_fast:Prefix dict has been built succesfully.\n",
      "############ 切分文本 ############\n",
      "实际输入的目标文本:\n",
      "。你好，我是一个测试文本。\n",
      "实际输入的目标文本(切句后):\n",
      "['你好，我是一个测试文本。']\n",
      "############ 提取文本Bert特征 ############\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 1/1 [00:00<00:00, 62.41it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "############ 推理 ############\n",
      "前端处理后的文本(每句): ['你好,我是一个测试文本.']\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  0%|          | 0/1500 [00:00<?, ?it/s]E:\\AItools\\GPT-SoVITS-Inference\\GPT_SoVITS\\AR\\models\\t2s_model.py:102: UserWarning: 1Torch was not compiled with flash attention. (Triggered internally at ..\\aten\\src\\ATen\\native\\transformers\\cuda\\sdp_utils.cpp:263.)\n",
      "  attn = F.scaled_dot_product_attention(q, k, v, attn_mask)\n",
      "  4%|▎         | 55/1500 [00:00<00:11, 120.80it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "T2S Decoding EOS [166 -> 222]\n",
      "############ 各阶段耗时: 处理参考音频 [1.895]s, 文本处理 [0.020]s, t2s_model [0.468]s, vits_model [0.355]s ############\n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "sys.path.append(\".\")\n",
    "\n",
    "from Adapters.gsv_fast import GSV_Instance \n",
    "from src.TTS_Task import TTS_Task\n",
    "\n",
    "gsv_instance = GSV_Instance()\n",
    "task = TTS_Task()\n",
    "\n",
    "task.character = \"Hutao\"\n",
    "task.text = \"你好，我是一个测试文本。\"\n",
    "\n",
    "\n",
    "gen = gsv_instance.generate_from_text(task)\n",
    "sr, audio = next(gen)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Set seed to 59318874\n",
      "分桶处理模式已开启\n",
      "实际输入的参考文本: 我说白术，你不会看不出来吧？难不成你师父，忘了教你这门功夫？\n",
      "############ 切分文本 ############\n",
      "实际输入的目标文本:\n",
      "我正在进行音频测试，下面是一段测试文本：蓝蓝的天空，白白的云，红红的太阳挂在天空中央。\n",
      "实际输入的目标文本(切句后):\n",
      "['我正在进行音频测试，下面是一段测试文本：', '蓝蓝的天空，白白的云，红红的太阳挂在天空中央。']\n",
      "############ 提取文本Bert特征 ############\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 2/2 [00:00<00:00, 57.91it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "############ 推理 ############\n",
      "前端处理后的文本(每句): ['我正在进行音频测试,下面是一段测试文本,']\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  6%|▌         | 89/1500 [00:00<00:11, 121.93it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "T2S Decoding EOS [166 -> 256]\n",
      "前端处理后的文本(每句): ['蓝蓝的天空,白白的云,红红的太阳挂在天空中央.']\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  9%|▊         | 128/1500 [00:01<00:12, 108.49it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "T2S Decoding EOS [166 -> 295]\n",
      "############ 各阶段耗时: 处理参考音频 [0.157]s, 文本处理 [0.036]s, t2s_model [1.183]s, vits_model [0.394]s ############\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "origin_audio_file =r\"E:\\AItools\\GPT-SoVITS-Inference\\models/gsv\\Hutao\\我说白术，你不会看不出来吧？难不成你师父，忘了教你这门功夫？.wav\"\n",
    "prompt_text = os.path.basename(origin_audio_file).replace(\".wav\", \"\")\n",
    "new_audio_file =r\"E:\\AItools\\声音合成一键运行240320\\Models\\银狼\\你吼那么大声干什么嘛.wav\"\n",
    "# audio_file =r\"E:\\AItools\\tr\\丁真\\我最近一直在努力学习.wav\"\n",
    "text = \"我正在进行音频测试，下面是一段测试文本：蓝蓝的天空，白白的云，红红的太阳挂在天空中央。\"\n",
    "\n",
    "\n",
    "prompt_cache:dict = {\n",
    "            \"ref_audio_path\" : None,\n",
    "            \"prompt_semantic\": None,\n",
    "            \"refer_spec\"     : None,\n",
    "            \"prompt_text\"    : None,\n",
    "            \"prompt_lang\"    : None,\n",
    "            \"phones\"         : None,\n",
    "            \"bert_features\"  : None,\n",
    "            \"norm_text\"      : None,\n",
    "        }\n",
    "\n",
    "params = {\n",
    "            \"text\": text,\n",
    "            \"ref_audio_path\": origin_audio_file,\n",
    "            \"text_lang\": \"zh\",\n",
    "            \"prompt_text\": prompt_text,\n",
    "            \"prompt_lang\": \"zh\",\n",
    "            \"top_k\": 3,\n",
    "            \"top_p\": 0.5,\n",
    "            \"temperature\": 0.5,\n",
    "            \"text_split_method\": \"auto_cut_100\", \n",
    "            \"batch_size\": 1,\n",
    "            \"speed_factor\": 1.0,\n",
    "            \"ref_text_free\": False,\n",
    "            \"split_bucket\":True,\n",
    "            \"return_fragment\":False,\n",
    "            \"seed\": -1,\n",
    "        }\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "gen = gsv_instance.tts_pipline.run(params)\n",
    "sr, audio = next(gen)\n",
    "\n",
    "\n",
    "import pyaudio\n",
    "\n",
    "import soundfile as sf\n",
    "import numpy as np\n",
    "import tempfile \n",
    "p = pyaudio.PyAudio()\n",
    "streamAudio = p.open(\n",
    "            format=p.get_format_from_width(2), channels=1, rate=32000, output=True\n",
    "        )\n",
    "streamAudio.write(audio.tobytes())\n",
    "streamAudio.stop_stream()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "import pyaudio\n",
    "\n",
    "import soundfile as sf\n",
    "import numpy as np\n",
    "import tempfile \n",
    "\n",
    "# audio : np.ndarray\n",
    "\n",
    "p = pyaudio.PyAudio()\n",
    "streamAudio = p.open(\n",
    "            format=p.get_format_from_width(2), channels=1, rate=32000, output=True\n",
    "        )\n",
    "streamAudio.write(audio.tobytes())\n",
    "streamAudio.stop_stream()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
